<?php

// $Id$


require_once($WSC->get_site_inc_dir().'modules/admin_panel/site_admin_web_page_wed_ua.class.php');
require_once($WSC->get_site_inc_dir().'modules/admin_panel/classes/admin_user/admin_user_dbc.class.php');

require_once($WSC->get_site_inc_dir().'modules/tree_menu/tree_menu_item_dbc.class.php');


class Menu_Manage_WP extends Site_Admin_Web_Page_WED_UA {
	public function init() {
		parent::init();

		$v = $this->create_view();
		$this->add_view($v);

		$prefix_event = new Web_Event_Simple('prefix', Tangra_Parameter_Method::GET, 'prefix');
		$this->wed->add_wed_event_action_pair($prefix_event, 'prefix_event', 30);

		$move_up_event = new Web_Event_Simple_Int('move_up', Tangra_Parameter_Method::GET, 'move_up');
		$move_up_event_comp = new Web_Event_Composite('move_up_event_comp');
		$move_up_event_comp->add_wes($move_up_event);
		$move_up_event_comp->add_wes($prefix_event);
		$this->wed->add_wed_event_action_pair($move_up_event_comp, 'move_up_event', 10);

		$move_down_event = new Web_Event_Simple_Int('move_down', Tangra_Parameter_Method::GET, 'move_down');
		$move_down_event_comp = new Web_Event_Composite('move_down_event_comp');
		$move_down_event_comp->add_wes($move_down_event);
		$move_down_event_comp->add_wes($prefix_event);
		$this->wed->add_wed_event_action_pair($move_down_event_comp, 'move_down_event', 20);
	}


	protected function prefix_event($prefix) {
		$menu_table = addslashes($prefix).'menu';


		$menu = new Tree_Menu_ML_DBC($menu_table, 1);
		$dbc = $this->get_dbc();
		if ($menu->load_menu($dbc, true)) {
			$this->export('_menu', $menu);
			$this->export('_menu_table', $menu_table);
		} else {
			throw new Tangra_User_Exception('Cannot load menu.');
		}

		$this->export('prefix', $prefix);

		$view = $this->get_view('default');


		return $view;
	}


	protected function move_up_event($params) {
		$dbc = $this->get_dbc();
		try {
			$dbc->start_trans();
			Tree_Menu_Item_DBC::move_up($dbc, $params['move_up'], $params['prefix'].'menu');
			$dbc->complete_trans();
		} catch (Exception $e) {
			$dbc->fail_trans();
			$dbc->complete_trans();
			throw $e;
		}

		$redir = $this->create_redirect_view('admin/menu/menu_manage.php', array('prefix' => $params['prefix']));

		return $redir;
	}


	protected function move_down_event($params) {
		$dbc = $this->get_dbc();
		try {
			$dbc->start_trans();
			Tree_Menu_Item_DBC::move_down($dbc, $params['move_down'], $params['prefix'].'menu');
			$dbc->complete_trans();
		} catch (Exception $e) {
			$dbc->fail_trans();
			$dbc->complete_trans();
			throw $e;
		}

		$redir = $this->create_redirect_view('admin/menu/menu_manage.php', array('prefix' => $params['prefix']));

		return $redir;
	}



	protected function default_event() {
		$view = $this->prefix_event('');

		return $view;
	}
}